package gestionCandidats;

import modele.Parcours;
import modele.Bd;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JButton;

import javax.swing.border.BevelBorder;

import javax.swing.table.TableModel;
import javax.swing.table.AbstractTableModel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.BorderLayout;

import java.util.SortedSet;

public class ParcoursAfficheAction extends AbstractAction  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GestionCandidats appli;
	private JPanel vue;
	protected ParcoursAfficheAction(GestionCandidats appli) {
		super("ParcoursAfficheAction");
		this.appli = appli;
		vue = appli.getParcoursAffiche();
	}

	public void actionPerformed(ActionEvent e) {
		try {
			if(vue == null) {
				vue = new JPanel();
				vue.setLayout(new BorderLayout());
				appli.add(vue,BorderLayout.CENTER);

				JPanel pied = new JPanel();
				vue.add(pied, BorderLayout.SOUTH);
				JButton ok = new JButton("Fermer");
				ok.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						vue.setVisible(false);
					}
				});
				pied.add(ok);
			}
			Bd bd = appli.getBd();
			final SortedSet<Parcours> parcours = bd.getParcours();
			final String[] names = Parcours.entete;
			final Object[][] data = new Object[parcours.size()][names.length];
			int i = 0;
			for (Parcours par : parcours) {
				data[i] = par.getInfo();
				i++;
			}
			// Create a model of the data.
			TableModel dataModel = new AbstractTableModel() {
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;
				// These methods always need to be implemented.
				public int getColumnCount() { return names.length; }
				public int getRowCount() { return parcours.size();}
				public Object getValueAt(int row, int col) {
					return data[row][col];
				}
				public void setValueAt(Object aValue, int row, int column){
					data[row][column] = aValue;
				}
				public String getColumnName(int column) {
					return names[column];
				}
				@SuppressWarnings({ "unchecked", "rawtypes" })
				public Class getColumnClass(int c) {
					return getValueAt(0, c).getClass();
				}
				public boolean isCellEditable(int row, int col) {
					return false;
				}
			};
			// Create the table
			JTable parcoursListe = new JTable(dataModel);
			parcoursListe.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
			parcoursListe.setRowHeight(2*parcoursListe.getRowHeight());
			// Finish setting up the table.
			JScrollPane scrollpane = new JScrollPane(parcoursListe);
			scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));
			vue.add(scrollpane,BorderLayout.CENTER);
			//scrollpane.setViewportView(parcoursListe);
			vue.setVisible(true);
			appli.getFrame().setVisible(true);
		}
		catch(Exception ex){
			JOptionPane.showMessageDialog(appli,ex.getMessage(),"Affichage des parcours : Attention",JOptionPane.ERROR_MESSAGE);
		}
	}
}

